﻿using System;
using System.Collections.Generic;
using System.Web;
using Gift.DAL;
using Gift.Model;
using AjaxPro;
using System.Data;
using System.Text;



namespace WebSite.Code
{
    public class CarGoodsInfo_ajax
    {

        public StringBuilder carinfo = new StringBuilder();
        public StringBuilder goodsInfo = new StringBuilder();
        readonly DALModelType theModelType = new DALModelType();
        tbModelType oModelType;
        BasePage thePage = new BasePage();
        readonly DALCar theCar = new DALCar();

        public string totalCardGoodsPoints = "0";
        public string totalGengerateGoodsPoints = "0";

        [AjaxMethod]
        public string GetCarProfInfo()
        {
            DataTable table = null;
            carinfo.Append("  <div class=\"car_goods_title\"><div>提货卡商品</div></div>"
                              + "   <table width=\"100%\" border=\"0\" cellspacing=\"0\" class=\"article_content\">"
                               + "   <tr> "
                               + "    <th width=\"90\" height=\"28\" align=\"center\">商品图片</th>"
                                + "   <th align=\"center\">商品名称</th>"
                                 + "  <th width=\"90\" align=\"center\">单件福点数</th>"
                                  + " <th width=\"90\" align=\"center\">数量</th>"
                                   + " <th width=\"90\" align=\"center\">共计福点数</th>"
                                  + " <th width=\"90\" align=\"center\"><strong>操作</strong></th>"
                                  + " </tr>");
	                           

            if (thePage.LoginUserMember != null)
            {
                if (thePage.LoginUserMember.IsCard)
                {
                    table = theCar.GetListByMemberId(long.Parse(thePage.LoginUserMember.TempMembertId), 1).Tables[0];
                }
            }
            else
            {
                if (re.ReadCookie("tempnoCard") != null)
                {
                    try
                    {
                        table = theCar.GetListByMemberId(long.Parse(re.ReadCookie("tempnoCard")), 1).Tables[0];
                    }
                    catch
                    {
                    }
                }
            }
            if (table != null)
            {
                string sModelTypeName = string.Empty ;

                foreach (DataRow row in table.Rows)
                {
                    if (row["modelTypeId"].ToString() != "0")
                    {
                        oModelType = theModelType.GetModel(int.Parse(row["modelTypeId"].ToString()));
                        if (oModelType != null)
                        {
                            sModelTypeName = "（" + oModelType.modelTypeName + "）";
                        }
                    }
                    totalCardGoodsPoints = (Convert.ToSingle(row["sumPoints"]) + Convert.ToSingle(this.totalCardGoodsPoints)).ToString();
                    carinfo.Append("<tr>");
                    carinfo.Append("<td align='center'><a href='/cn/goodsDetail.html?gid=" + row["goodsId"].ToString() + "'><img src='../uploadfile/product/" + row["smallPic"].ToString() + "' align='left' border='0'  width='106' /></a></td>");
                    carinfo.Append("<td align='left' class='article_content'><a href='/cn/goodsDetail.html?gid=" + row["goodsId"].ToString() + "' class='blue'>" + row["goodsName"].ToString() + "</a>" + sModelTypeName + "</td>");
                    carinfo.Append("<td align='center'>" + row["goodsPoints"].ToString() + "  </td>");
                    carinfo.AppendFormat("<td align='center'><a  onclick='updateCarGoodsCount({2},{0})'><b>[-]</b></a>&nbsp;<font color='red' size='5'>{1}</font>&nbsp;<a  onclick='updateCarGoodsCount(1,{0})'><b>[+]</b></a></td>" 
                        , row["carId"].ToString()
                        , row["goodsCount"].ToString()
                        , int.Parse(row["goodsCount"].ToString()) <= 1 ? "0" : "-1"
                     
                        );
                    carinfo.Append("<td align='center'>" + row["sumPoints"].ToString() + "  </td>");
                    carinfo.AppendFormat("<td align='center'><a href='#' onclick='deleteCarGoods({0});'>[删除]</a></td>"
                                             , row["carId"].ToString());
                    carinfo.Append("</tr>");
                }
            }
             carinfo.Append(    "<tr>"
	                              +"  <td height=\"1\" colspan=\"6\" align=\"center\" background=\"images/pro/line_vbg1.gif\"><img src=\"images/pro/space.gif\" width=\"1\" height=\"1\" /></td>"
	                              +"  </tr>"
	                            + " </table>"
                                +" <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
                                +"  <tr>"
                                 +"   <td bgcolor=\"#F4FDF7\"></td>"
                                 + "  <td height=\"30\" bgcolor=\"#F4FDF7\">总计：<span id=\"totalCardGoodsPoints\" class=\"text_red\">{*****.**}</span><span class=\"a_title_green\"> 福点 </span></td>"
                                 +" </tr>"
		                         +" <tr>"
                                  + "  <td width=\"77%\" height=\"1\" bgcolor=\"#1E69A7\"></td>"
                                  + "  <td width=\"23%\" bgcolor=\"#1E69A7\"></td>"
                                 +" </tr>"
                               +" </table>");
            return carinfo.ToString().Replace("{*****.**}", totalCardGoodsPoints);
        }

        [AjaxMethod]
        public string GetCarGenerateInfo()
        {
            this.goodsInfo.Append("    <div class=\"car_goods_title\"><div>通用卡类商品</div></div> "
                                       + " <table width=\"100%\" border=\"0\" cellspacing=\"0\" class=\"article_content\">"
                                       + " <tr> "
                                       + " <th width=\"90\" height=\"28\" align=\"center\">商品图片</th>"
                                       + " <th align=\"center\">商品名称</th>"
                                       + " <th width=\"90\" align=\"center\">单件福点数</th>"
                                       + " <th width=\"90\" align=\"center\">数量</th>"
                                       + " <th width=\"90\" align=\"center\">共计福点数</th>"
                                       + " <th width=\"90\" align=\"center\"><strong>操作</strong></th>"
                                       + " </tr>");
                                     


            DataTable tbCardGoods = null;
            if (thePage.LoginUserMember != null)
            {
                tbCardGoods = theCar.GetListByMemberId(long.Parse(thePage.LoginUserMember.MemberId), 0).Tables[0];
            }
            else
            {

                if (re.ReadCookie("tempnoGenerate") != null)
                {
                    try
                    {
                        tbCardGoods = theCar.GetListByMemberId(long.Parse(re.ReadCookie("tempnoGenerate")), 0).Tables[0];
                    }
                    catch
                    {
                    }
                }
            }

            if (tbCardGoods != null)
            {
                string sModelTypeName = string.Empty;
                foreach (DataRow row in tbCardGoods.Rows)
                {
                    if (row["modelTypeId"].ToString() != "0")
                    {
                        oModelType = theModelType.GetModel(int.Parse(row["modelTypeId"].ToString()));
                        if (oModelType != null)
                        {
                            sModelTypeName = "（" + oModelType.modelTypeName + "）";
                        }
                    }

                    this.totalGengerateGoodsPoints = (Convert.ToSingle(row["sumPoints"]) + Convert.ToSingle(this.totalGengerateGoodsPoints)).ToString();
                    this.goodsInfo.Append("<tr>");
                    this.goodsInfo.Append("<td align='center'><a href='/cn/goodsDetail.html?gid=" + row["goodsId"].ToString() + "'><img src='../uploadfile/product/" + row["smallPic"].ToString() + "' align='left' border='0'  width='106' /></a></td>");
                    this.goodsInfo.Append("<td align='left' class='article_content'><a href='/cn/goodsDetail.html?gid=" + row["goodsId"].ToString() + "' class='blue'>" + row["goodsName"].ToString() + "</a>" + sModelTypeName + "</td>");
                    this.goodsInfo.Append("<td align='center'>" + row["goodsPoints"].ToString() + "  </td>");
                    this.goodsInfo.AppendFormat("<td align='center'><a  onclick='updateCarGoodsCount({2},{0})'><b>[-]</b></a>&nbsp;<font color='red' size='5'>{1}</font>&nbsp;<a  onclick='updateCarGoodsCount(1,{0})'><b>[+]</b></a></td>"
                       , row["carId"].ToString()
                       , row["goodsCount"].ToString()
                       , int.Parse(row["goodsCount"].ToString()) <= 1 ? "0" : "-1"

                       );
                    this.goodsInfo.Append("<td align='center'>" + row["sumPoints"].ToString() + "  </td>");
                    this.goodsInfo.AppendFormat("<td align='center'><a href='#' onclick='deleteCarGoods({0});'>[删除]</a></td>"
                                                , row["carId"].ToString()
                                                );
                    this.goodsInfo.Append("</tr>");
                }

            }

                

            this.goodsInfo.Append(" <tr>"
                                       + " <td height=\"1\" colspan=\"6\" align=\"center\" background=\"images/pro/line_vbg1.gif\"><img src=\"images/pro/space.gif\" width=\"1\" height=\"1\" /></td>"
                                       + "  </tr>"
                                       + "  </table>"
                                      + "  <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
                                      + "  <tr>"
                                      + "  <td bgcolor=\"#F4FDF7\"></td>"
                                      + "  <td height=\"30\" bgcolor=\"#F4FDF7\">总计：<span id=\"totalGengerateGoodsPoints\" class=\"text_red\">{*****.**}</span><span class=\"a_title_green\"> 福点 </span></td>"
                                      + "  </tr>"
                                      +" <tr>"
                                       + "  <td width=\"77%\" height=\"1\" bgcolor=\"#1E69A7\"></td>"
                                  + "  <td width=\"23%\" bgcolor=\"#1E69A7\"></td>"
                                  +"</tr>"
                                      + "  </table>");
            return this.goodsInfo.ToString().Replace("{*****.**}", totalGengerateGoodsPoints);

        }
        [AjaxMethod]
        public string GetTotalCardGoodsPoints()
        {
            return totalCardGoodsPoints;
        }

        [AjaxMethod]
        public string GetTotalGengerateGoodsPoints()
        {
            return totalGengerateGoodsPoints;
        }
    }
}